package com.lee.ssm.controller;

import com.lee.ssm.domain.Permission;
import com.lee.ssm.domain.Role;
import com.lee.ssm.service.IPermissionService;
import com.lee.ssm.service.IProductService;
import com.lee.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IPermissionService iPermissionService;
    @RequestMapping("/findAll")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
       List<Role> list= iRoleService.findAll();
       mv.addObject("roleList",list);
       mv.setViewName("role-list");
       return mv;
    }
    @RequestMapping("/save")
    public String save(Role role) throws Exception {
        iRoleService.save(role);
        return "redirect:findAll.do";
    }
    @RequestMapping("/findById")
    public ModelAndView findById(String id) throws Exception{
        ModelAndView mv=new ModelAndView();
        Role role=iRoleService.findById(id);
        mv.addObject("role",role);
        mv.setViewName("role-show");
        return mv;
    }
    @RequestMapping("/deleteRole")
    public String deleteRole(String id) throws Exception{

        iRoleService.deleteRole(id);
        return  "redirect:findAll.do";
    }
    @RequestMapping("/findRoleByIdAndAllPermission")
    public ModelAndView findRoleByIdAndAllPermission(String id) throws Exception {
       ModelAndView mv=new ModelAndView();
        Role role = iRoleService.findById(id);
        List<Permission> permissionList= iPermissionService.findOtherPermissionByRoleId(id);
        mv.addObject("role",role);
        mv.addObject("permissionList",permissionList);
        mv.setViewName("role-permission-add");
        return mv;
    }
    @RequestMapping("/addPermissionToRole")
    public String addPermissionToRole(@RequestParam("roleId") String roleId,@RequestParam("ids") String[] permissionId ) throws Exception {
        iRoleService.addPermissionToRole(roleId,permissionId);
        return "redirect:findById.do?id="+roleId;
    }
}
